package com.uphone.magicleafteacher.pro.activity;

import android.arch.lifecycle.LifecycleOwner;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.radish.baselibrary.Intent.IntentData;
import com.radish.baselibrary.Intent.IntentUtils;
import com.radish.baselibrary.view.FullyLinearLayoutManager;
import com.radish.framelibrary.view.glide.GlideUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.pro.adapter.ImageAdapter;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.HomeWorkDetailBean;
import com.uphone.magicleafteacher.vo.WorkcorrectionEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

public class WorkCorrectionActivity extends BaseGActivity {
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.text_student_name)
    TextView textStudentName;
    @BindView(R.id.text_teacher_age)
    TextView textTeacherAge;
    @BindView(R.id.text_student_class)
    TextView textStudentClass;
    @BindView(R.id.lv_information)
    LinearLayout lvInformation;
    @BindView(R.id.text_work_name)
    TextView textWorkName;
    @BindView(R.id.text_live_courses)
    TextView textLiveCourses;
    @BindView(R.id.linear)
    LinearLayout linear;
    @BindView(R.id.text_yu)
    TextView textYu;
    @BindView(R.id.img_preview)
    ImageView imgPreview;
    @BindView(R.id.text_integral)
    TextView textIntegral;
    @BindView(R.id.img_jobresult)
    ImageView imgJobresult;
    @BindView(R.id.iv_sex)
    ImageView ivSex;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.tv_request)
    TextView tvRequest;
    @BindView(R.id.rl_teacher)
    RelativeLayout rlTeacher;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.recyclerView1)
    RecyclerView recyclerView1;
    @IntentData
    private String integral;
    @IntentData
    private int status;
    @IntentData
    private int processId;
    private String[] workPic;
    private String[] remarkPic;
    private ImageAdapter mAdapter1;
    private ImageAdapter mAdapter;

    @Override
    protected void initLayoutBefore() {
        super.initLayoutBefore();
        isEventBus = true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefresh(WorkcorrectionEvent outGroupEvent) {
        finish();
        //刷新
//        initData();
    }


    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_work_correct;
    }

    @Override
    protected void initView() {
        if (status == 1) {
            btnSubmit.setVisibility(View.VISIBLE);
            rlTeacher.setVisibility(View.GONE);
        } else {
            btnSubmit.setVisibility(View.GONE);
            rlTeacher.setVisibility(View.VISIBLE);
        }

        recyclerView.setLayoutManager(new FullyLinearLayoutManager(this));
        recyclerView1.setLayoutManager(new FullyLinearLayoutManager(this));
        mAdapter = new ImageAdapter();
        mAdapter1 = new ImageAdapter();
        recyclerView.setAdapter(mAdapter);
        recyclerView1.setAdapter(mAdapter1);
    }

    @Override
    protected void initTitle() {
        initTitle("详情");
    }

    @Override
    protected void initData() {
        MyApp.apiService(ApiService.class)
                .homeworkDetail("" + processId)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    showLoading();
                })
                .doFinally(() -> {
                    closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) this)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        HomeWorkDetailBean bean = GsonUtils.getGson().fromJson(jsonObject, HomeWorkDetailBean.class);
                        if (bean != null && bean.getWorkdetail() != null) {
                            loadData(bean.getWorkdetail());
                        }
                    }
                }, new FailureConsumer());
    }

    private void loadData(HomeWorkDetailBean.WorkdetailBean workdetail) {
        if (workdetail.getProcessStatus() == 1) {
            btnSubmit.setVisibility(View.VISIBLE);
            rlTeacher.setVisibility(View.GONE);
        } else {
            btnSubmit.setVisibility(View.GONE);
            rlTeacher.setVisibility(View.VISIBLE);
        }
        GlideUtils.getInstance().loadCircleImage(this, CommonUtil.getImageUrl(workdetail.getHeadPhoto()), ivHead);
        if (workdetail.getGender() == 1) {
            Glide.with(this).load(R.drawable.ic_man).into(ivSex);
        } else {
            Glide.with(this).load(R.drawable.ic_woman).into(ivSex);
        }
        textStudentName.setText(CommonUtil.getStr(workdetail.getStudentName()));
        textTeacherAge.setText(workdetail.getAge() + "岁");
        textStudentClass.setText(CommonUtil.getStr(workdetail.getGradeName()));
        textWorkName.setText(CommonUtil.getStr(workdetail.getTitle()));
        textIntegral.setText(CommonUtil.getStr(workdetail.getJifen() + ""));
        textLiveCourses.setText("来源于:《" + CommonUtil.getStr(workdetail.getClassName()) + "》");
        tvRequest.setText("作业要求：" + CommonUtil.getStr(workdetail.getRequest()));
        workPic = workdetail.getMyWorkPic();
//        if (workPic != null && workPic.length != 0) {
//            GlideUtils.getInstance().loadCornerImage(this, CommonUtil.getImageUrl(workPic[0]), SizeUtils.dp2px(8), imgPreview);
//        }
        mAdapter.setNewData(Arrays.asList(workPic));
        remarkPic = workdetail.getRemark();
//        if (remarkPic != null && remarkPic.length != 0) {
//            GlideUtils.getInstance().loadCornerImage(this, CommonUtil.getImageUrl(remarkPic[0]), SizeUtils.dp2px(8), imgJobresult);
//        }
        mAdapter1.setNewData(Arrays.asList(remarkPic));
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.img_preview, R.id.img_jobresult, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_preview:
                CommonUtil.openBigPic(CommonUtil.getImageUrl(workPic[0]));
                break;
            case R.id.img_jobresult:
                CommonUtil.openBigPic(CommonUtil.getImageUrl(remarkPic[0]));
                break;
            case R.id.btn_submit:
                if (workPic == null || workPic.length == 0) {
                    ToastUtils.showShort("暂无作业可批改");
                    return;
                }
                for (int i = 0; i < workPic.length; i++) {
                    workPic[i] = CommonUtil.getImageUrl(workPic[i]);
                }
                IntentUtils.getInstance()
                        .with(this, WorkcorrectioningActivity.class)
                        .putInt("id", processId)
                        .putStrings("filePaths",workPic)
//                        .putString("saveFilePath", getFilesDir() + "temp.png")
                        .start();
                break;
        }
    }
}
